package cn.tedu.sample.controller;

import cn.tedu.sample.entity.User;
import cn.tedu.sample.mapper.UserMapper;
import cn.tedu.sample.util.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("user")
public class UserController {

    @Autowired(required = false)
    private UserMapper userMapper;

    // http://localhost:8080/user/reg?username=sample&password=8888&age=26&phone=13100131111&email=sample@baidu.com
    @RequestMapping("reg")
    public JsonResult reg(User user) {
        System.err.println("UserController.reg"); // soutm
        System.err.println("user = " + user); // soutp

        String username = user.getUsername();
        User result = userMapper.findByUsername(username);
        // result.null
        JsonResult jsonResult = new JsonResult();
        if (result == null) {
            userMapper.insert(user);
            jsonResult.setState(1);
            jsonResult.setMessage("注册成功！");
        } else {
            jsonResult.setState(2);
            jsonResult.setMessage("注册失败！用户名已经被占用！");
        }
        return jsonResult;
    }

}
